package liuliu.throughwaring.ui.fragment;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.IBinder;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.List;

import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseFragment;
import liuliu.custom.model.CodeModel;
import liuliu.throughwaring.model.Key;
import liuliu.throughwaring.model.PoliceModel;
import liuliu.throughwaring.model.TerminalModel;
import liuliu.throughwaring.service.UpdateMain;
import liuliu.throughwaring.ui.activity.CongyeAndZulinActivity;
import liuliu.throughwaring.ui.activity.MainActivity;
import liuliu.throughwaring.ui.activity.NewCompanyListActivity;
import liuliu.throughwaring.ui.activity.OnLineSearchActivity;
import liuliu.throughwaring.ui.activity.SearchNoticeList;
import liuliu.throughwaring.ui.activity.PoliceAndLiuDongActivity;
import liuliu.throughwaring.ui.activity.WaringActivity;

/**
 * 作者：柳伟杰 on 2016/3/14 15:15
 * 邮箱：1031066280@qq.com
 */
public class ShouYeFragment extends BaseFragment {
    @CodeNote(id = R.id.congye_shouye_ll, click = "onClick")
    LinearLayout congye_shouye_ll;
    @CodeNote(id = R.id.liudong_shouye_ll, click = "onClick")
    LinearLayout liudong_shouye_ll;
    @CodeNote(id = R.id.zulin_shouye_ll, click = "onClick")
    LinearLayout zulin_shouye_ll;
    @CodeNote(id = R.id.minjing_shouye_ll, click = "onClick")
    LinearLayout minjing_shouye_ll;
    @CodeNote(id = R.id.waring_shouye_ll, click = "onClick")
    LinearLayout waring_shouye_ll;
    @CodeNote(id = R.id.lianwang_shouye_ll, click = "onClick")
    LinearLayout lianwang_shouye_ll;
    @CodeNote(id = R.id.main_company_list, click = "onClick")
    LinearLayout companyList;
    @CodeNote(id = R.id.main_notice, click = "onClick")
    LinearLayout mainNotice;
    @CodeNote(id = R.id.shouye_notice_num)
    TextView noticeNum;
    @CodeNote(id = R.id.shouye_company_num, click = "onClick")
    TextView companyNum;
    @CodeNote(id = R.id.shouye_area)
    TextView area;
    @CodeNote(id = R.id.congye_waring_ll)
    ImageView congye_waring_ll;
    UpdateMain.UpdateMainBind binder = null;
    Utils mUtils;

    @Override
    public void initViews() {
        setContentView(R.layout.frag_shouye);
        mUtils = new Utils(MainActivity.mIntails);
        Intent intent = new Intent(MainActivity.mIntails, UpdateMain.class);
        MainActivity.mIntails.bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    Bitmap map;

    @Override
    public void initEvents() {
        List<TerminalModel> list = MainActivity.mIntails.finalDb.findAllByWhere(TerminalModel.class, "TerminalId='" + Utils.getImei(MainActivity.mIntails) + "'");
        if (list.size() > 0) {
            area.setText(MainActivity.mIntails.getCodeVlaue(MainActivity.mIntails.codeList, list.get(0).getTerminalArea()));
        }
//        congye_waring_ll.addView(new DrawView(MainActivity.mIntails));
        map = Bitmap.createBitmap(120, 120, Bitmap.Config.ARGB_8888);
        Canvas cvs = new Canvas(map);
        Paint p = new Paint();
        p.setColor(Color.RED);// 设置红色
        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(10);
        cvs.drawCircle(60, 60, 50, p);
        congye_waring_ll.setImageBitmap(map);
        area.setText(Utils.ReadString(Key.KEY_STATION_NAME));
    }

    /**
     * 使用内部类 自定义一个简单的View
     *
     * @author Administrator
     */
    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder iBinder) {
            binder = (UpdateMain.UpdateMainBind) iBinder;
            if (binder != null) {
                binder.Refush(new UpdateMain.GetNoticeFace() {
                    @Override
                    public void FaceShow(int companyCount, int noticeCount) {
                        companyNum.setText(companyCount + "条");
                        noticeNum.setText(noticeCount + "条");
                    }
                });
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.congye_shouye_ll://从业人员
                mUtils.IntentPost(CongyeAndZulinActivity.class, new Utils.putListener() {
                    @Override
                    public void put(Intent intent) {
                        intent.putExtra("style", "congye");
                    }
                });
                break;
            case R.id.liudong_shouye_ll://流动人员
                mUtils.IntentPost(PoliceAndLiuDongActivity.class, new Utils.putListener() {
                    @Override
                    public void put(Intent intent) {
                        intent.putExtra("style", "liudong");
                    }
                });
                break;
            case R.id.zulin_shouye_ll://租赁人员
                mUtils.IntentPost(CongyeAndZulinActivity.class, new Utils.putListener() {
                    @Override
                    public void put(Intent intent) {
                        intent.putExtra("style", "zulin");
                    }
                });
                break;
            case R.id.minjing_shouye_ll://民警管理
                mUtils.IntentPost(PoliceAndLiuDongActivity.class, new Utils.putListener() {
                    @Override
                    public void put(Intent intent) {
                        intent.putExtra("style", "police");
                    }
                });
                break;
            case R.id.waring_shouye_ll://报警比对
                mUtils.IntentPost(WaringActivity.class);
                break;
            case R.id.main_company_list:
                mUtils.IntentPost(NewCompanyListActivity.class);
                break;
            case R.id.main_notice:
                mUtils.IntentPost(SearchNoticeList.class);
                break;
            case R.id.lianwang_shouye_ll://联网查询
                mUtils.IntentPost(OnLineSearchActivity.class);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MainActivity.mIntails.unbindService(conn);
    }
}
